import React from 'react';
import { message } from 'antd';
// import { history } from 'umi'
// import cookie from 'js-cookie';
import { useRequest } from '@/hooks';
// import { fakeAccountLogin } from '@/services/api';
import { fakeAccountLogin } from '@/pages/RecruitEmail/service/interview';
import { pushRoute } from '../utils';

const useLogin = (query, failCallback) => {
    const [needAuthCode, setNeedAuthCode] = React.useState(false);
    const { loading, run: handleLogin } = useRequest(
        params =>
            fakeAccountLogin({
                username: params.talentEmail,
                password: params.password,
                client_id: '1422402458692440065',
                client_secret: '899730c05b2562b3f2e7fd2b400dfd06',
                verificationKey: params.verificationKey,
                verificationValue: params.verificationValue
            }),
        {
            manual: true,
            initialData: null,
            onSuccess: res => {
                console.log('onSuccess', res);
                // const { data: resDataTemp, response } = res || {};
                const { code, msg, data } = res || {};
                if (code !== 10000 && failCallback) {
                    failCallback();
                }
                if (code === 10000) {
                    localStorage.setItem('candidateUserCode', data?.userCode);
                    // cookie.set('Authorization', response.headers.get('Authorization') || '', {
                    //     expires: 30
                    // });
                    localStorage.setItem('loginInfo', JSON.stringify(data));
                    const { redirect, ...rest } = query;
                    if (redirect) {
                        pushRoute({
                            pathname: redirect,
                            query: {
                                ...rest
                            }
                        });
                    } else {
                        // history.goBack();
                        // 跳转到首页
                        pushRoute({
                            pathname: '/recruitment',
                            query: {
                                ...rest
                            }
                        });
                    }
                } else if (res.code === 20100 || res.code === 110201 || res.code === 20010) {
                    setNeedAuthCode(true);
                    if (res.code === 20010) {
                        message.error(msg);
                    }
                } else if (msg) {
                    message.error(msg);
                }
            }
        }
    );

    return {
        needAuthCode,
        handleloading: { loading },
        handleLogin
    };
};

export default useLogin;
